define(function (require, exports) {
	var pubjs = require('pubjs');
	var $ = require('jquery');
	var tip = require('@base/tip');
	var util = require('util');
	var common = require('modules/common');

	// 弹出tip
	var Tip = tip.base.extend({
		init: function (config) {
			config = pubjs.conf(config, {
				'pos': 'ml',
				'width': 500,
				'offsetX': -5,
				'autoShow': false,
				'autoHide': 'click_body',
				'hasArrow': true,
				'url': 'qualification/info',
				'class': 'P-adminAptitudeList'
			});

			this.$rows = {};
			this.Super('init', arguments);
		},
		afterBuild: function () {
			var self = this;
			self.Super('afterBuild', arguments);
			self.css('display', 'none');
			var con = self.getContainer();

			con.addClass('pr');

			var table = $([
				'<div class="P-adminAptitudeListResultTip h400">',
				'<div class="tipContent">',
				'<table class="table table-stripe">',
				'<thead>',
				'<tr>',
				'<th class="tc col1">' + LANG('渠道') + '</th>',
				'<th class="">' + LANG('审核状态') + '</th>',
				'</tr>',
				'</thead>',
				'<tbody />',
				'</table>',
				'</div>',
				'</div>'
			].join('')).appendTo(con);
			var tbody = table.find('tbody');

			var map = pubjs.config('channelMap');
			util.each(pubjs.config('aptitude_group'), function (exchange_id) {
				var tr = $('<tr />').appendTo(tbody);
				var exchange = $('<td />').appendTo(tr);
				var status = $('<td />').appendTo(tr);

				exchange.text(LANG(map[exchange_id])).addClass('tc');
				self.$rows[exchange_id] = {
					tr: tr,
					exchange: exchange,
					status: status
				}
			});

			if (util.isMobile()) {
				table.css('overflow-y', 'auto');
			} else {
				// 构建滚动条
				self.createAsync('scroll', '@base/common/base.scroller', {
					'height': 365,
					'content': table.find('.tipContent'),
					'target': table,
					'watch': true,
					'dir': 'V'
				});
			}
		},
		load: function (id) {
			this.showLoading();
			pubjs.mc.send(this.getConfig('url'), {_id: id}, this.onData.bind(this));
			return this;
		},
		onData: function (err, data) {
			this.hideLoading();
			if (err) {
				pubjs.alert(err.message);
			}
			if (data && data.Info) {
				this.setContentData(data.Info);
			}
			this.updateScroll();
		},
		setContentData: function (data) {
			var self = this;
			// 过滤历史脏数据，可能存在一个渠道可能存在多个记录
			var items = common.processQualificationInfo(data);
			var infoMap = {};
			util.each(items, function (item) {
				if (item && !item.IsDeleted) {
					infoMap[item.ExchangeId] = item;
				}
			});

			util.each(self.$rows, function (row, exchange_id) {
				row.status.html(self.renderRow(infoMap[exchange_id]));
			});
		},
		renderRow: function (item) {
			if (item && !item.IsDeleted) {
				var text;
				switch (item.Status) {
					case 2 :
						text = LANG("部分通过") + '：' + (util.html(item.RejectReason) || '-');
						return '<div title="' + LANG("部分通过") + '" class="clear"><span class="icon done fl mr5" />' + text + '</div>';
					case 1 :
						return '<div title="' + LANG("通过") + '" class="clear"><span class="icon icon-checkbox-in font-success fl mr5" />' + LANG("通过") + '</div>';
					case 0 :
						return '<div title="' + LANG("待审核") + '" class="clear"><span class="icon uk-icon-clock-o fl mr5" />' + LANG("待审核") + '</div>';
					case -2 :
						return '<div title="' + LANG("审核中") + '" class="clear"><span class="icon uk-icon-clock-o fl mr5" />' + LANG("审核中") + '</div>';
					case -1 :
						var klazz = (item.RejectReason && item.RejectReason.indexOf('系统判断：') === 0) ? 'sys' : 'err';
						text = LANG("未通过") + '：' + (util.html(item.RejectReason) || '-');
						return '<div title="' + text + '" class="clear" ><span class="icon rejectReason icon-close font-danger fl mr5 ' + klazz + '" />' + text + '</div>';
					default:
						return '-';
				}
			} else {
				return '-';
			}
		},
		updateScroll: function () {
			var scroll = this.get('scroll');
			if (scroll) {
				scroll.update();
			}
		},
		show: function () {
			this.Super('show', arguments);
			return this;
		}
	});

	exports.main = Tip;
});